package com.flea.hclv.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.flea.system.bean.FleaPage;

import java.io.Serializable;


// 保证序列化json时候 ，如果对象是null，key也会消失
public class ServerResponse<T> implements Serializable {


    /**
     *
     */
    private static final long serialVersionUID = 9044313823400353700L;
    // 现在T为某个类型
    private int status;
    private String message;
    private T data;
    //HashMap<String, Object> model = new HashMap<String, Object>();
    JSONObject model = new JSONObject();



/*	public HashMap<String, Object> getModel() {
		return model;
	}

	public void setModel(HashMap<String, Object> model) {
		this.model = model;
	}*/


    private ServerResponse(int status) {

        this.status = status;
        model.put("status", status);
    }


    public JSONObject getModel() {
        return model;
    }


    public void setModel(JSONObject model) {
        this.model = model;
    }


    @JsonIgnore
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private ServerResponse(int status, T data) {

        if (null != data && data.getClass().getName().equals(FleaPage.class.getName())) {
            String s = JSON.toJSONStringWithDateFormat(data, "yyyy-MM-dd  HH:mm:ss");
            this.model = JSONObject.parseObject(s);
            this.data = data;
            this.status = status;
        } else {
            this.status = status;
            this.data = data;
            model.put("status", status);
            model.put("data", data);
        }


    }

    private ServerResponse(int status, String message, T data) {
        if (null != data && data.getClass().getName().equals(FleaPage.class.getName())) {
            String s = JSON.toJSONStringWithDateFormat(data, "yyyy-MM-dd  HH:mm:ss");
            this.model = JSONObject.parseObject(s);
            this.status = status;
            this.data = data;
            this.message = message;
        } else {
            this.status = status;
            this.data = data;
            this.message = message;
            model.put("status", status);
            model.put("data", data);
            model.put("message", message);
        }


    }

    private ServerResponse(int status, String message) {
        this.status = status;
        this.message = message;
        model.put("status", status);
        model.put("message", message);

    }


    // 使之json 序列化之后不会显在结果中
    @JsonIgnore
    public boolean isSuccess() {
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    @JsonIgnore
    public int getStatus() {
        return status;
    }



/*	public String getMessage() {
		return message;
	}*/

    public static <T> ServerResponse<T> createBySuccess() {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String message) {
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode(), message);
    }

    /**
     * 这个<T> T 可以传入任何类型的 参数T 第一个 表示是泛型 第二个 表示返回的是T类型的数据 第三个 限制参数类型为T
     *
     * @param model
     * @return
     */
    public static <T> ServerResponse<T> createBySuccess(T model) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), model);
    }

    public static <T> ServerResponse<T> createBySuccess(String message, T model) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), message, model);
    }

    public static <T> ServerResponse<T> createByError() {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),
                ResponseCode.ERROR.getDesc());
    }

    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <T> ServerResponse<T> createByErrorCodeMassage(int errorCode,
                                                                 String errorMessage) {
        return new ServerResponse<T>(errorCode, errorMessage);
    }

    @JsonIgnore
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
